// packageX/business_district_management/activity_audit/activity_audit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activities: [],
    showRejectPopup: false,
    showDetailPopup: false,
    currentActivityId: '',
    currentActivity: {},
    rejectReason: '',
    districtId: '',
    pageNum: 1,
    pageSize: 10,
    hasMore: true,
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 可以从options中获取districtId
    if (options && options.districtId) {
      this.setData({
        districtId: options.districtId
      });
    }
    // 调用API获取活动列表
    this.getActivityList(true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // 下拉刷新时重置页码和数据
    this.setData({
      pageNum: 1,
      hasMore: true
    });
    this.getActivityList(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 上拉加载更多
    if (this.data.hasMore && !this.data.loading) {
      this.getActivityList(false);
    }
  },

  /**
   * 获取活动列表
   */
  getActivityList(isRefresh = false) {
    if (this.data.loading) return;

    this.setData({ loading: true });

    const app = getApp();
    let urlStr = app.getNetAddresss('plugin.commerce.api.commerce-activity.get-audit-activity-list');
    app._getNetWork({
      url: urlStr,
      data: {
        district_id: this.data.districtId,
        page: this.data.pageNum,
        page_size: this.data.pageSize
      },
      success: (res) => {
        let newData = [];
        
        if (isRefresh || this.data.pageNum === 1) {
          // 刷新或第一页，直接替换数据
          newData = res.data.list || [];
        } else {
          // 加载更多，追加数据
          newData = [...this.data.activities, ...(res.data.list || [])];
        }

        const hasMore = newData.length < res.data.total;

        this.setData({
          activities: newData,
          hasMore: hasMore,
          loading: false
        });

        if (hasMore) {
          // 如果还有更多数据，页码+1
          this.setData({
            pageNum: this.data.pageNum + 1
          });
        }
      },
      fail: (err) => {
        console.error('获取活动列表失败:', err);
        this.setData({ loading: false });
        wx.showToast({
          title: '获取活动列表失败，请重试',
          icon: 'none'
        });
      },
      complete: () => {
        wx.stopPullDownRefresh();
      }
    });
  },

  /**
   * 查看活动详情
   */
  onActivityTap(e) {
    const id = e.currentTarget.dataset.id;
    const activity = this.data.activities.find(item => item.id === id);
    if (activity) {
      this.setData({
        showDetailPopup: true,
        currentActivity: activity
      });
    }
  },

  /**
   * 拒绝活动点击事件
   */
  onRejectTap(e) {
    const id = e.currentTarget.dataset.id;
    this.setData({
      showRejectPopup: true,
      currentActivityId: id,
      rejectReason: ''
    });
  },

  /**
   * 通过活动点击事件
   */
  onApproveTap(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认通过',
      content: '确定要通过这个活动吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '处理中' });
          
          // 调用实际的API通过活动
          const app = getApp();
          app._getNetWork({
            url: 'plugin.commerce.api.commerce-activity.audit-activity',
            data: {
              activity_id: id,
              state: 1 // 1表示通过
            },
            success: (res) => {
              // 操作成功后重新获取列表
              this.setData({
                pageNum: 1,
                hasMore: true
              });
              this.getActivityList(true);
              
              wx.showToast({
                title: '已通过活动',
                icon: 'success'
              });
            },
            fail: (err) => {
              console.error('通过活动失败:', err);
              wx.showToast({
                title: '操作失败，请重试',
                icon: 'none'
              });
            },
            complete: () => {
              wx.hideLoading();
            }
          });
        }
      }
    });
  },

  /**
   * 拒绝理由输入变化
   */
  onRejectReasonChange(e) {
    this.setData({
      rejectReason: e.detail.value
    });
  },

  /**
   * 关闭拒绝弹窗
   */
  onCloseRejectPopup() {
    this.setData({
      showRejectPopup: false,
      rejectReason: ''
    });
  },

  /**
   * 关闭详情弹窗
   */
  onCloseDetailPopup() {
    this.setData({
      showDetailPopup: false,
      currentActivity: {}
    });
  },

  /**
   * 确认拒绝
   */
  onConfirmReject() {
    if (!this.data.rejectReason.trim()) {
      wx.showToast({
        title: '请输入拒绝理由',
        icon: 'none'
      });
      return;
    }
    
    wx.showLoading({ title: '处理中' });
    
    // 调用实际的API拒绝活动
    const app = getApp();
    app._getNetWork({
      url: 'plugin.commerce.api.commerce-activity.audit-activity',
      data: {
        activity_id: this.data.currentActivityId,
        state: -1, // -1表示不通过
        comment: this.data.rejectReason
      },
      success: (res) => {
        // 关闭弹窗
        this.onCloseRejectPopup();
        
        // 操作成功后重新获取列表
        this.setData({
          pageNum: 1,
          hasMore: true
        });
        this.getActivityList(true);
        
        wx.showToast({
          title: '已拒绝活动',
          icon: 'success'
        });
      },
      fail: (err) => {
        console.error('拒绝活动失败:', err);
        wx.showToast({
          title: '操作失败，请重试',
          icon: 'none'
        });
      },
      complete: () => {
        wx.hideLoading();
      }
    });
  }
});